package cn.e3mall.activemq;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

/**
 * 使用spring整合activemq
 * @author zhouzhixiang
 *
 */
public class ActiveMpSpring {

	public void sendMessage(){
		//初始化spring容器
		ApplicationContext context=new ClassPathXmlApplicationContext(""
				+ "classpath:spring/applicationContext-activemq.xml");
		//从容器中获得JmsTemplate对象
		JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
		//从容器中得到一个Destination对象
		Destination destination = (Destination) context.getBean("queueDestination"); 
		//发送消息
		jmsTemplate.send( destination, new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				// TODO Auto-generated method stub
				return session.createTextMessage("send activemq message");
				
			}
		});
		
	}
}
